﻿
namespace Anycmd.Xacml.Tests
{
    using Context;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Policy;
    using Runtime;
    using System.IO;

    public class Common
    {
        public static void Test(string[] files)
        {
            Assert.AreEqual(files.Length, 3);
            FileInfo policyFile = new FileInfo(Consts.Path + files[0]);
            FileInfo requestFile = new FileInfo(Consts.Path + files[1]);
            FileInfo responseFile = new FileInfo(Consts.Path + files[2]);
            using (FileStream fs = new FileStream(policyFile.FullName, FileMode.Open, FileAccess.Read))
            using (FileStream fs1 = new FileStream(requestFile.FullName, FileMode.Open, FileAccess.Read))
            using (FileStream fs2 = new FileStream(responseFile.FullName, FileMode.Open, FileAccess.Read))
            {
                // Load Policy
                PolicyDocument policyDocument = (PolicyDocument)PolicyLoader.LoadPolicyDocument(fs, XacmlVersion.Version20, DocumentAccess.ReadOnly);
                // Load Request
                ContextDocumentReadWrite requestDocument = ContextLoader.LoadContextDocument(fs1, XacmlVersion.Version20);
                // Load ResponseElement
                ContextDocumentReadWrite responseDocument = ContextLoader.LoadContextDocument(fs2, XacmlVersion.Version20);
                EvaluationEngine engine = new EvaluationEngine();

                ResponseElement res = engine.Evaluate(policyDocument, (ContextDocument)requestDocument);
                Assert.AreEqual(responseDocument.Response.Results[0].Obligations.Count, res.Results[0].Obligations.Count);
                Assert.AreEqual(responseDocument.Response.Results.Count, res.Results.Count);
                for (int i = 0; i < responseDocument.Response.Results.Count; i++)
                {
                    Assert.AreEqual(res.Results[i].Decision, responseDocument.Response.Results[i].Decision);
                }
                Assert.AreEqual(responseDocument.Response.Results[0].Decision.ToString(), res.Results[0].Decision.ToString(), string.Format("Decission incorrect Expected:{0} Returned:{1}", responseDocument.Response.Results[0].Decision, res.Results[0].Decision));
                Assert.AreEqual(responseDocument.Response.Results[0].Status.StatusCode.Value, res.Results[0].Status.StatusCode.Value, string.Format("Status incorrect Expected:{0} Returned:{1}", responseDocument.Response.Results[0].Status.StatusCode.Value, res.Results[0].Status.StatusCode.Value));
            }
        }
    }
}
